home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d13 / pcrjan89.arc / KEYCODE.ASM < prev    next >
Assembly Source File  |  1990-03-21  |  2KB  |  109 lines

  1. comment |
  2.    This short program displays the full scan code for each key pressed 
  3.    in ASCII/Hex notation.  Press ESC to end the program.
  4.  
  5.    Written for MASM 5.1 (should compile with earlier versions).
  6.    Save as KEYCODE.ASM
  7.    Compile:  MASM KEYCODE;
  8.             LINK KEYCODE;
  9.        |
  10. stack    segment stack
  11.     db    100h dup (?)
  12. stack    ends
  13.  
  14. data    segment
  15. hextable    db    "0123456789ABCDEF"
  16. data    ends
  17.  
  18. code    segment
  19.     assume cs:code, ds:data, es:nothing, ss:stack
  20.     
  21.  
  22. ;---------
  23. ;  Pick the definition
  24. ;  that applies to your
  25. ;  computer
  26. ;---------
  27.     
  28. KEY_IN    equ    00h        ;For older keyboards
  29. ;KEY_IN    equ    10h        ;For "enhanced" keyboards (101/102 keys)
  30.  
  31. ;---------
  32. ;  Program starts here
  33. ;---------
  34.  
  35. begin:    mov    ax,seg data    ;Initialize DS
  36.     mov    ds,ax
  37.     call    cls        ;Clear the screen
  38.     
  39. lp:    mov    ah,KEY_IN    ;Wait for keystroke
  40.     int    16h        ;  returned by BIOS
  41.     push    ax        ;Save the key
  42.     call    display        ;Display the scan code
  43.     pop    ax        ;Recover original key
  44.     cmp    al,01bh        ;Was it ESC?
  45.     jnz    lp        ;No -- loop back
  46.     mov    ax,4c00h    ;Else exit to DOS
  47.     int    21h
  48.  
  49. ;---------
  50. ;  Simple and sloppy
  51. ;  screen-clear routine
  52. ;---------
  53.     
  54. cls    proc    near
  55.     mov    ax,3        ;Set mode 3 (use 7 for monochrome)
  56.     int    10h        ;Call Video BIOS
  57.     ret
  58. cls    endp
  59.     
  60. ;---------
  61. ;  Display the value
  62. ;  in AX in hex notation
  63. ;  by moving each byte into AL
  64. ;  and calling print_al twice
  65. ;---------
  66.  
  67. display    proc    near        ;Display AX value in hex
  68.     push    ax        ;Save keycode
  69.     mov    ah,02        ;Set cursor position
  70.     mov    dx,0        ;Top-left corner
  71.     mov    bh,0        ;Use page 0
  72.     int    10h        ;Call the video BIOS
  73.     pop    ax        ;Recover original keycode
  74.  
  75. disp_2:    push    ax        ;Save keycode again
  76.     mov    al,ah        ;Move high byte into AL
  77.     call    print_al    ;Print value in AL
  78.     pop    ax        ;Recover keycode
  79.     call    print_al    ;Print low byte
  80.     ret
  81. display    endp
  82.  
  83. ;---------
  84. ;  Display the value in AL
  85. ;  in hex format
  86. ;---------
  87.  
  88. print_al proc    near
  89.     mov    bx,offset hextable  ;DS:BX ==> translation table
  90.     cbw            ;AH = 0
  91.     push    ax        ;Save the byte
  92.     mov    cl,4        ;Shift top nibble in AL
  93.     shr    al,cl        ;  to the bottom
  94.     xlat            ;Get hex character
  95.     mov    ah,0eh        ;Video service: print TTY mode
  96.     int    10h        ;Print the character
  97.     pop    ax        ;Recover the byte
  98.     and    al,0fh        ;Mask out high nibble
  99.     xlat            ;Translate low nibble to hex
  100.     mov    ah,0eh        ;And print it
  101.     int    10h        ;  with a BIOS call
  102.     mov    al,' '        ;Now print a space
  103.     int    10h        ;  with a BIOS call
  104.     ret
  105. print_al endp
  106.  
  107. code    ends
  108.     end    begin
  109.